package service

import (
	"dta-apk/global"
	"dta-apk/huawei"
	"dta-apk/model"
	"dta-apk/oppo"
	"dta-apk/platapi"
	"dta-apk/vivo"
	"dta-apk/xiaomi"
	"fmt"
	"time"
)

func StartAppCheckInfoTicker(appId, versionId, packageName string) {
	stop := make(chan bool)
	// 创建定时器，10秒触发一次
	timer := time.NewTicker(10 * time.Second)
	// 启动goroutine，在定时器触发时执行操作
	go func() {
		for {
			select {
			case <-timer.C:
				fmt.Println("开始定向获取应用审核情况")
				result := getPublishCheckInfo(appId, versionId, packageName)
				if result {
					stop <- true
				}

			case <-stop:
				// 停止定时器
				timer.Stop()
				return
			}
		}
	}()
}

/*
查询发布版本的各平台审核情况
@param appId 应用id
@param versionId 版本id
@param packageName 应用包名
*/
func getPublishCheckInfo(appId, versionId, packageName string) bool {
	var versionData model.VersionModel
	result := model.FindById(&versionData, versionId)
	if !result {
		return true
	}
	// 查询应用版本成功发布的平台
	var publishes []model.AppPublishModel
	result = model.FindAll(&publishes, &model.AppPublishModel{VersionId: versionId, PublishStatus: 1})
	// 查询平台是否已经审核完成
	isAllChecked := 0
	publishPlatCount := 0
	if result {
		publishPlatCount = len(publishes)
		for _, publish := range publishes {
			// 平台类型
			plat := publish.Plat
			// 查询是否有该平台的审核记录, 如果不存在，就获取审核结果
			if publish.CheckStatus == "" {
				// 获取对应平台的审核信息
				publishResult := GetCheckInfoOfPlate(packageName, plat)
				if publishResult.VersionCode == publish.VersionCode {
					// 审核完成后保存审核信息
					if publish.CheckStatus != "3" {
						publish.CheckDesc = publishResult.CheckDesc
						publish.CheckStatus = publishResult.CheckStatus
						publish.VersionCode = publishResult.VersionCode
						// 保存数据
						model.Update(&publish)
					}
				}
			} else {
				isAllChecked += 1
			}
		}
	}
	return isAllChecked == publishPlatCount && publishPlatCount != 0

}

/*
获取平台的最新审核结果
@param packageName 应用包名
@param plat 平台类型
*/
func GetCheckInfoOfPlate(packageName, plat string) model.AppPublishModel {
	var publishResult model.AppPublishModel
	if plat == platapi.Oppo {
		// 查询oppo的审核结果
		oppoResult, isResult := oppo.GetAppDetails(packageName, "")
		if isResult {
			// 判断审核状态
			if oppoResult.AuditStatus == "111" || oppoResult.AuditStatus == "2" {
				// 上线，审核通过
				publishResult.CheckStatus = "1"
			} else if oppoResult.AuditStatus == "444" || oppoResult.AuditStatus == "3" {
				// 审核不通过, 测试不通过
				publishResult.CheckStatus = "2"
			} else if oppoResult.AuditStatus == "1" || oppoResult.AuditStatus == "00" {
				// 审核中
				publishResult.CheckStatus = "3"
			} else if oppoResult.AuditStatus == "222" || oppoResult.AuditStatus == "22" {
				// 已冻结.下线
				publishResult.CheckStatus = "4"
			}
			// 审核不通过原因
			publishResult.CheckDesc = oppoResult.RefuseReason
			// 版本号
			publishResult.VersionCode = oppoResult.VersionName
		}
	} else if plat == platapi.Vivo {
		// 查询vivo的审核结果
		vivoResult, isResult := vivo.GetAppDetails(packageName)
		if isResult {
			vivoStatus := vivoResult.Status
			// 判断状态
			if vivoStatus == 4 {
				// 审核不通过
				publishResult.CheckStatus = "2"
			} else if vivoStatus == 3 {
				// 审核通过
				publishResult.CheckStatus = "1"
			} else {
				// 审核中
				publishResult.CheckStatus = "3"
			}
			// 审核不通过原因
			publishResult.CheckDesc = vivoResult.UnPassReason
			// 版本号
			publishResult.VersionCode = vivoResult.VersionName
		}
	} else if plat == platapi.Xiaomi {
		// 查询小米审核结果
		xiaomiResult, isResult := xiaomi.GetAppDetails(packageName, global.Config.App.XiaoMi.UserName)
		if isResult {
			// 若为版本可更新，就是审核完成
			if xiaomiResult.UpdateVersion {
				// 如果一上线的版本和发布的版本相同，发布成功
				publishResult.CheckStatus = "1"
			} else {
				// 审核中
				publishResult.CheckStatus = "3"
			}
			// 版本号
			publishResult.VersionCode = xiaomiResult.PackageInfo.VersionName
		}
	} else if plat == platapi.Huawei {
		// 查询华为审核结果
		huaweiResult, isResult := huawei.GetAppDetails(packageName)
		if isResult {
			// 审核状态
			huaweiState := huaweiResult.AppInfo.ReleaseState
			if huaweiState == 1 || huaweiState == 8 || huaweiState == 9 {
				publishResult.CheckStatus = "2"
				// 拒绝原因
				publishResult.CheckDesc = huaweiResult.AuditInfo.AuditOpinion
			} else if huaweiState == 0 {
				publishResult.CheckStatus = "1"
			} else {
				// 审核中
				publishResult.CheckStatus = "3"
			}
			// 版本号
			publishResult.VersionCode = huaweiResult.AppInfo.VersionNumber
		}
	}
	return publishResult
}
